package algotithm.acwing.dp.test1015;

/**
 * @author 25068
 * @Date 2021/6/26 9:17
 */
import java.io.*;

public class Main {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));

        int x = Integer.parseInt(reader.readLine());
        while (x-- > 0) {
            String[] s = reader.readLine().split(" ");
            int r = Integer.parseInt(s[0]);
            int c = Integer.parseInt(s[1]);
            int[][] arr = new int[r][c];
            int[][] dp = new int[r + 1][c + 1];
            int t = r;
            int idx = 0;
            while (t-- > 0) {
                String[] str = reader.readLine().split(" ");
                for (int i = 0 ; i < c; i++) {
                    arr[idx][i] = Integer.parseInt(str[i]);
                }
                idx++;
            }
            for (int i = 1; i <= r; i++) {
                for (int j = 1; j <= c; j++) {
                    dp[i][j] = Math.max(dp[i - 1][j],dp[i][j - 1]) + arr[i - 1][j - 1];
                }
            }
            writer.write(dp[r][c] + "\n");
        }
        writer.flush();
        writer.close();
        reader.close();
    }
}